/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData;

import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.InterfacePropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.overall.Utils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import org.eclipse.jdt.core.dom.StringLiteral;

public class AssoUdtItemPropertyGenerator extends InterfacePropertyGenerator {

  private String parentPropName;
  private String propName;
  private DataTypeDataGeneratorContext dataGenContext;
  private IGspCommonField field;

  public AssoUdtItemPropertyGenerator(
      String parentPropName, IGspCommonField field, DataTypeDataGeneratorContext dataGenContext) {
    this.parentPropName = parentPropName;
    this.propName = field.getLabelID();
    this.field = field;
    this.dataGenContext = dataGenContext;
  }

  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
    TypeInfo t = getPropertyType();
    dataGenContext.getPropertyInfos().put(getPropertyName(),
        new MediatePropertyInfo(getPropertyName()) {{
          setElement(field);
          setPropertyType(t);
          setExtendInfo("AssoUdtAtrribute", parentPropName);
        }}
    );
  }

  @Override
  protected String getPropertyName() {
    if (parentPropName.equals(propName)) {
      return propName + "_ID";
    }
    return propName;
  }

  @Override
  protected TypeInfo getPropertyType() {
    if (!field.getIsRefElement()) {
      addImport(BigDecimal.class.getTypeName());
      addImport(Date.class.getTypeName());
      return Utils.getNativeType(field,
          dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getCefGenContext());

    }
    return Utils.getRefElementPropertyName(
        field,
        field.getParentAssociation(),
        dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getCefGenContext());
  }

  @Override
  protected String getFieldName() {
    return null;
  }

  @Override
  protected boolean hasSetMethod() {
    return false;
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    ArrayList<AnnotationInfo> list = new ArrayList<>();

    AnnotationInfo info = new AnnotationInfo();
    info.setAnnotationType(AnnotationType.Normal);
    TypeInfo typeInfo = new TypeInfo("AssoUdtAtrribute");
    typeInfo.setTypePackageName("com.inspur.edp.cef.api.attr");
    info.setTypeInfo(typeInfo);
    StringLiteral param = ast.newStringLiteral();
    param.setLiteralValue(parentPropName);
    info.getParams().put("parentPropName", param);

    list.add(info);
    return list;
  }


}
